﻿Imports iTextSharp.text
Imports iTextSharp.text.pdf

''' <summary>
''' This class implements a writer that generates a PDF file.
''' </summary>
''' <remarks>
''' The PDF writer creates a simple PDF output file.
''' </remarks>
Public Class PDFWriter
    Implements IWriter

    ''' <summary>
    ''' Writer for PDF output.
    ''' </summary>
    ''' <remarks></remarks>
    Protected PDF As iTextSharp.text.pdf.PdfWriter

    ''' <summary>
    ''' PDF document.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Doc As New Document

    ''' <summary>
    ''' Output file.
    ''' </summary>
    ''' <remarks></remarks>
    Protected fName As String

    Public Sub Cleanup() Implements IWriter.Cleanup
        'Close the output file.
        Doc.Close()
        Doc = Nothing
    End Sub

    Public Sub Startup(ByVal cfgFile As String) Implements IWriter.Startup

        'Read the configuration from the XML file.
        Dim ds As New DataSet
        ds.ReadXml(cfgFile)

        'Get the file to output.
        fName = Convert.ToString(ds.Tables(0).Rows(0).Item("ReportOutput"))

        'Perform replacements.
        fName = fName.Replace("$YYYY$", Now.Year.ToString())
        fName = fName.Replace("$MM$", Now.Month.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$DD$", Now.Day.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$HH$", Now.Hour.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$mm$", Now.Minute.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$SS$", Now.Second.ToString().PadLeft(2, "0"c))

        'Open the file.
        Doc = New Document
        PDF = iTextSharp.text.pdf.PdfWriter.GetInstance(Doc, New IO.FileStream(fName, IO.FileMode.Create))
        Doc.Open()

        ds.Dispose()
        ds = Nothing

    End Sub

    Public Sub WriteBlank() Implements IWriter.WriteBlank
        AddWriting(" ", 10)
    End Sub

    Public Sub WriteBlankEntry() Implements IWriter.WriteBlankEntry
        AddWriting(" ", 7)
    End Sub

    Public Sub WriteCategory(ByVal str As String) Implements IWriter.WriteCategory
        AddWriting(str, 10, iTextSharp.text.Color.BLUE, True, True)
    End Sub

    Public Sub WriteCategoryError(ByVal str As String) Implements IWriter.WriteCategoryError
        AddWriting(str, 10, iTextSharp.text.Color.RED, True, True)
    End Sub

    Public Sub WriteEntry(ByVal str As String) Implements IWriter.WriteEntry
        AddWriting("    " + str, 8)
    End Sub

    Public Sub WriteEntry(ByVal id As String, ByVal value As String) Implements IWriter.WriteEntry
        AddWriting("    " + id.PadRight(25, " "c) + "   " + value, 7)
    End Sub

    Public Sub WriteEntryError(ByVal str As String) Implements IWriter.WriteEntryError
        AddWriting("    " + str, 7, iTextSharp.text.Color.RED)
    End Sub

    Public Sub WriteFooter() Implements IWriter.WriteFooter
        AddWriting(New String("-"c, 40), 10, , , True)
    End Sub

    Public Sub WritePageBreak() Implements IWriter.WritePageBreak
        Doc.NewPage()
    End Sub

    Public Sub WriteSection(ByVal str As String) Implements IWriter.WriteSection
        AddWriting("  " + str, 9, iTextSharp.text.Color.DARK_GRAY, True)
    End Sub

    Public Sub WriteSectionError(ByVal str As String) Implements IWriter.WriteSectionError
        AddWriting("  " + str, 9, iTextSharp.text.Color.RED, True)
    End Sub

    Public Sub WriteSystem(ByVal str As String) Implements IWriter.WriteSystem
        AddWriting(str, 12, True, True, True)
    End Sub

    Public Sub WriteTitle(ByVal str As String) Implements IWriter.WriteTitle
        AddWriting(str, 12, True, True, True)
    End Sub

    Public Sub WriteVersion(ByVal str As String) Implements IWriter.WriteVersion
        AddWriting("v." + str, 10, True, , True)
        WriteBlank()
        WriteBlank()
    End Sub

    Private Sub AddWriting(ByVal str As String, ByVal size As Integer, _
                           Optional ByVal bold As Boolean = False, _
                           Optional ByVal underlined As Boolean = False, _
                           Optional ByVal center As Boolean = False)
        AddWriting(str, size, iTextSharp.text.Color.BLACK, bold, underlined, center)
    End Sub

    Private Sub AddWriting(ByVal str As String, ByVal size As Integer, _
                           ByVal color As iTextSharp.text.Color, _
                           Optional ByVal bold As Boolean = False, _
                           Optional ByVal underlined As Boolean = False, _
                           Optional ByVal center As Boolean = False)
        Dim f As Font = Nothing
        Dim fStyle As Integer = Font.NORMAL
        If bold Then fStyle = fStyle Or Font.BOLD
        If underlined Then fStyle = fStyle Or Font.UNDERLINE
        f = FontFactory.GetFont(FontFactory.COURIER, size, fStyle, color)
        Dim c As Chunk = New Chunk(str, f)
        Dim p As New Paragraph(str, f)
        If center Then p.Alignment = Element.ALIGN_CENTER
        Doc.Add(p)
    End Sub

    Public Function GetOutputFile() As String Implements IWriter.GetOutputFile
        Return fName
    End Function

End Class
